﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HouseHoldingRunService.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the HouseHoldingRunService type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Services
{
  using System;
  using System.Collections.Generic;
  using BusinessEngineInterfaces;
  using Entities;
  using Exceptions;
  using ServiceInterfaces;
  using UnityRepository.Interfaces;

  public class HouseHoldingRunService : IHouseHoldingRunService
  {
    private readonly IHouseHoldingRunRepository houseHoldingRunRepository;
    private readonly IUserService userService;

    public HouseHoldingRunService(IHouseHoldingRunRepository houseHoldingRunRepository, IUserService userService)
    {
      this.houseHoldingRunRepository = houseHoldingRunRepository;
      this.userService = userService;
    }

    public HouseHoldingRun CreateHouseHoldingRun(String houseHoldingGrid, DateTime startDate, DateTime endDate)
    {
      try
      {
        var houseHoldingRun = new HouseHoldingRun
        {
          Grid = houseHoldingGrid,
          StartDate = startDate,
          EndDate = endDate
        };

        return this.houseHoldingRunRepository.Create(houseHoldingRun);
      }
      catch (Exception e)
      {
        throw new UnityException("Could not create householding run", e);
      }
    }

    public IList<HouseHoldingRun> GetTopFifteenRecentHouseHeldGrids()
    {
      try
      {
        return this.houseHoldingRunRepository.GetTopFifteenRecentHouseHeldGrids(this.userService.GetUserManCoIds());
      }
      catch (Exception e)
      {
        throw new UnityException("Unable to retrieve top 10 recent unapproved grids", e);
      }
    }

    public IList<HouseHoldingRun> GetTopFifteenRecentHouseHeldGrids(List<Int32> manCoIds)
    {
      try
      {
        return this.houseHoldingRunRepository.GetTopFifteenRecentHouseHeldGrids(manCoIds);
      }
      catch (Exception e)
      {
        throw new UnityException("Unable to retrieve top 10 recent unapproved grids", e);
      }
    }
  }
}
